﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CommonClasses.Utility
{
    public partial class AskIntegerValueForm : Form
    {
        static int value;
        private AskIntegerValueForm(string caption, string text, int existValue, int min, int max)
        {
            InitializeComponent();
            Text = caption;
            label1.Text = text;
            numericUpDown1.Minimum = min;
            numericUpDown1.Maximum = max;
            value = existValue;
            numericUpDown1.Value = existValue;
        }

        public static int Ask(string caption, string text, int existValue, int min, int max)
        {
            var form = new AskIntegerValueForm(caption, text, existValue, min, max);
            form.ShowDialog();
            return value;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            value = (int)numericUpDown1.Value;
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                value = (int)numericUpDown1.Value;
                Close();
                return;
            }
            if (e.KeyData == Keys.Escape)
                Close();
        }
    }
}
